<template>
  <div class="user-event">
    <div class="container" v-if="code == 200">
      <div class="user-hd">
        <div class="user-hd-pic">
          <img :src="user.avatarUrl" :alt="user.nickname" class="user-pic" />
        </div>
        <div class="user-hd-main">
          <div class="user-name-box">
            <h1 class="user-name">{{ user.nickname }}</h1>
            <span class="level">lv{{ level }}</span>
            <span class="gender-male" v-if="user.gender == 1">
              <i class="iconfont">&#xec4c;</i>
            </span>
            <span class="gender-female" v-if="user.gender == 2">
              <i class="iconfont">&#xec4d;</i>
            </span>
            <span class="user-btn send" v-if="showUserBtn">发私信</span>
            <span
              :class="[
                'user-btn',
                { 'user-btn-gray': user.followed },
                'follow',
              ]"
              v-if="showUserBtn"
            >
              <span v-if="user.followed">已关注</span>
              <span v-if="!user.followed">关注</span>
            </span>
            <span
              class="user-btn artist"
              v-if="user.artistId"
              @click="clickArtist(user.artistId)"
            >
              查看歌手页
            </span>
            <span class="user-btn edit" v-if="!showUserBtn">编辑个人资料</span>
          </div>
          <ul class="user-info">
            <li class="user-info-item">
              <a href="javascript:;" @click="clickEvent(user.userId)">
                <strong>{{ user.eventCount }}</strong>
                <span>动态</span>
              </a>
            </li>
            <li class="user-info-item">
              <a href="javascript:;" @click="clickFollows(user.userId)">
                <strong>{{ user.follows }}</strong>
                <span>关注</span>
              </a>
            </li>
            <li class="user-info-item">
              <a href="javascript:;" @click="clickFans(user.userId)">
                <strong>{{ user.followeds }}</strong>
                <span>粉丝</span>
              </a>
            </li>
          </ul>
          <div class="user-signature" v-if="showSignature">
            个人介绍：{{ user.signature }}
          </div>
          <div class="user-message">
            <span class="user-place" v-if="user.province != 0">
              所在地区：{{ user.province | showCity(user.city) }}
            </span>
            <span class="user-age" v-if="user.birthday && user.birthday > 0">
              年龄：{{ user.birthday | showAge }}
            </span>
          </div>
        </div>
      </div>
      <div class="user-event-main">
        <div class="user-event-main-hd">
          <h2 class="user-event-main-txt">{{ who }}的动态({{ size }})</h2>
        </div>
        <div class="user-event-main-box clearfix">
          <div class="user-event-main-box-left">
            <ul class="user-event-infinite-list">
              <li
                v-for="item in event"
                :key="item.id"
                class="user-event-infinite-list-item"
              >
                <event :item="item"></event>
              </li>
              <p class="event-loading-box">
                <span class="event-loading" v-if="loading">
                  <i class="iconfont">&#xe610;</i>
                  加载中...
                </span>
              </p>
            </ul>
          </div>
          <div class="user-event-main-box-right">
            <div class="ard-relative-box" v-if="showFollows">
              <h2 class="ard-relative-hd">{{ who }}的关注</h2>
              <ul class="ard-fans clearfix">
                <li
                  class="ard-fans-item"
                  v-for="item in follows"
                  :key="item.userId"
                >
                  <a
                    class="ard-artist-pic"
                    href="javascript:;"
                    :title="item.nickname"
                    @click="clickUser(item.userId)"
                  >
                    <img :src="item.avatarUrl" :alt="item.nickname" />
                  </a>
                  <h4 class="ard-fans-name">
                    <a
                      class="ard-fans-txt"
                      href="javascript:;"
                      :title="item.nickname"
                      @click="clickUser(item.userId)"
                    >
                      {{ item.nickname }}</a
                    >
                  </h4>
                </li>
              </ul>
            </div>
            <div class="ard-relative-box" v-if="showFans">
              <h2 class="ard-relative-hd">{{ who }}的粉丝</h2>
              <ul class="ard-fans clearfix">
                <li
                  class="ard-fans-item"
                  v-for="item in fans"
                  :key="item.userId"
                >
                  <a
                    class="ard-artist-pic"
                    href="javascript:;"
                    :title="item.nickname"
                    @click="clickUser(item.userId)"
                  >
                    <img :src="item.avatarUrl" :alt="item.nickname" />
                  </a>
                  <h4 class="ard-fans-name">
                    <a
                      class="ard-fans-txt"
                      href="javascript:;"
                      :title="item.nickname"
                      @click="clickUser(item.userId)"
                    >
                      {{ item.nickname }}</a
                    >
                  </h4>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    <no-resource v-if="code == 404"></no-resource>
  </div>
</template>

<script>
import { changeAge } from "common/age.js";
import { city } from "common/city.js";
import Event from "components/event/Event";
import NoResource from "views/notfound/NoResource";

export default {
  components: {
    Event,
    NoResource,
  },
  data() {
    return {
      id: "",
      user: {},
      level: 1,
      showSignature: false,
      // 发私信、关注、编辑个人资料
      showUserBtn: true,
      who: "TA",
      // 动态
      size: 0,
      lasttime: "",
      event: [],
      time: null,
      more: true,
      loading: false,
      // 标记，防止连续加载
      timer: null,
      follows: [],
      showFollows: false,
      fans: [],
      showFans: false,
      // scrollLoad: null,
      code: 200,
    };
  },
  filters: {
    // 过滤器转换时间戳
    showAge(data) {
      return changeAge(data);
    },
    showCity(p, c) {
      return city(p, c);
    },
  },
  mounted() {
    let that = this;
    this.id = this.$route.query.id;
    this.getUserDetail();
    this.getUserEvent();
    this.getUserFollows();
    this.getUserFans();
    if (this.$store.state.profile.userId == this.id) {
      this.showUserBtn = false;
    } else {
      this.showUserBtn = true;
    }
    document.addEventListener("scroll", that.scrollLoad);
  },
  beforeDestroy() {
    let that = this;
    // this.scrollLoad = null;
    // this.more = false;
    // console.log("-----------------");
    // console.log(that.scrollLoad);
    document.removeEventListener("scroll", that.scrollLoad);
    // console.log(that.scrollLoad);
  },
  methods: {
    scrollLoad() {
      // 卷上去的高度
      let topHeight =
        document.documentElement.scrollTop ||
        window.pageYOffset ||
        document.body.scrollTop;
      //documentHeight 为可视窗口的高度
      let documentHeight = document.documentElement.clientHeight;
      //bodyHeight body对象的高度
      let bodyHeight = document.body.clientHeight;
      if (bodyHeight - topHeight - 300 < documentHeight && this.more) {
        if (!this.timer) {
          console.log(this.i++);
          this.loading = true;
          this.timer = setTimeout(() => {
            this.getUserEvent();
          }, 1000);
        }
      }
    },
    clickArtist(id) {
      this.playArtistDetail(id);
    },
    clickUser(id) {
      this.playUserDetail(id);
    },
    clickEvent(id) {
      this.playUserEvent(id);
    },
    clickFollows(id) {
      this.playUserFollows(id);
    },
    clickFans(id) {
      this.playUserFans(id);
    },
    getUserFans() {
      this.axios({
        /* 歌手详情接口 */
        url: "/user/followeds?limit=8&uid=" + this.id,
      })
        .then((res) => {
          this.fans = res.data.followeds;
          // console.log(this.fans);
          if (this.fans.length > 0) {
            this.showFans = true;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getUserFollows() {
      this.axios({
        /* 歌手详情接口 */
        url: "/user/follows?limit=8&uid=" + this.id,
      })
        .then((res) => {
          this.follows = res.data.follow;
          // console.log(this.follows);
          if (this.follows.length > 0) {
            this.showFollows = true;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getUserEvent() {
      this.axios({
        url: "/user/event?uid=" + this.id + "&lasttime=" + this.lasttime,
      })
        .then((res) => {
          this.lasttime = res.data.lasttime;
          this.event.push(...res.data.events);
          console.log(this.event);
          this.more = res.data.more;
          this.loading = false;
          this.timer = null;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getUserDetail() {
      this.axios({
        url: "/user/detail?uid=" + this.id,
      })
        .then((res) => {
          if (res.data.code == 200) {
            this.user = res.data.profile;
            this.level = res.data.level;
            this.size = res.data.profile.eventCount;
            if (res.data.profile.signature != "No introduction") {
              this.showSignature = true;
            }
            if (this.showUserBtn) {
              this.who = "TA";
            } else {
              this.who = "我";
            }
          }
          if (res.data.code == 404) {
            this.code = 404;
          }
        })
        .catch((err) => {
          console.log(err);
          this.code = 404;
        });
    },
  },
};
</script>

<style>
.user-event {
  padding-top: 50px;
  background-color: var(--color-theme-light-5);
}
.user-event .user-hd {
  position: relative;
  min-height: 250px;
  margin-bottom: 50px;
}
.user-event .user-hd-pic {
  position: absolute;
  left: 0;
  top: 0;
  width: 250px;
  height: 250px;
  padding: 5px;
  border: 2px solid #ccc;
}
.user-event .user-pic {
  width: 100%;
  height: 100%;
}
/* user-hd-main */
.user-event .user-hd-main {
  position: relative;
  margin-left: 300px;
  min-height: 250px;
  overflow: hidden;
}
.user-event .user-name-box {
  position: relative;
  height: 80px;
  line-height: 80px;
  border-bottom: 1px solid #999;
}
.user-event .user-name {
  float: left;
  margin: 0;
  font-size: 26px;
  height: 80px;
  line-height: 80px;
  font-weight: 400;
}
.user-event .user-name-box .level {
  margin-left: 20px;
  padding: 1px 5px;
  color: red;
  border: 1px solid red;
  border-radius: 15px;
}
.user-name-box .gender-male .iconfont,
.user-name-box .gender-female .iconfont {
  margin-left: 10px;
  font-size: 26px;
}
.user-name-box .gender-male {
  display: inline-block;
  height: 30px;
  line-height: 30px;
  color: rgb(38, 166, 228);
}
.user-name-box .gender-female {
  display: inline-block;
  height: 30px;
  line-height: 30px;
  color: rgb(255, 190, 216);
}
.user-event .user-name-box .send,
.user-event .user-name-box .follow {
  margin-left: 20px;
}
.user-event .user-name-box .artist,
.user-event .user-name-box .edit {
  float: right;
  margin-top: 24px;
  margin-left: 10px;
  line-height: 16px;
}
.user-event .user-name-box .user-btn {
  padding: 6px 8px;
  color: #000;
  background-color: #fff;
  border-radius: 5px;
  border: 1px solid #999;
}
.user-event .user-name-box .user-btn.user-btn-gray {
  color: #ccc;
  background-color: #f2f2f2;
  border: 1px solid #ccc;
}
.user-event .user-info {
  margin: 20px 0;
  width: 100%;
  height: 50px;
}
.user-event .user-info-item {
  float: left;
  padding: 0 10px;
  min-width: 80px;
  height: 50px;
  text-align: center;
  border-right: 1px solid #999;
}
.user-event .user-info-item a {
  display: inline-block;
  position: relative;
  color: #666;
}
.user-event .user-info-item:hover a {
  color: var(--color-theme);
}
.user-event .user-info-item strong {
  display: block;
  font-size: 24px;
  cursor: pointer;
}
.user-event .user-info-item span {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  cursor: pointer;
}
.user-event .user-signature {
  min-height: 50px;
  line-height: 22px;
  font-size: 14px;
  white-space: normal;
}
.user-event .user-message {
  height: 30px;
  line-height: 30px;
}
.user-event .user-message .user-age {
  margin-left: 20px;
}
/* user-event-main */
.user-event-main {
  position: relative;
  width: 100%;
}
.user-event .user-event-main-hd {
  position: relative;
  height: 50px;
  line-height: 50px;
  border-bottom: 2px solid var(--color-theme);
}
.user-event .user-event-main-txt {
  float: left;
  font-weight: 400;
}
.user-event .user-event-main-box {
  width: 100%;
  margin-bottom: 20px;
}
.user-event .user-event-main-box-left {
  position: relative;
  float: left;
  width: 850px;
}
.user-event-infinite-list-item {
  padding: 20px 0;
  border-bottom: 1px solid #999;
}

.event-loading-box {
  /* position: relative; */
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.event-loading {
  /* position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); */
  height: 20px;
  line-height: 20px;
  font-size: 16px;
}

.event-loading .iconfont {
  animation: loading 2s linear infinite;
  display: inline-block;
  font-size: 20px;
}
@keyframes loading {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
/* user-event-main-box-right */
.user-event .user-event-main-box-right {
  float: right;
  margin-top: 20px;
  width: 300px;
}
.ard-relative-box {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.ard-relative-hd {
  margin-bottom: 10px;
  font-weight: 400;
  height: 36px;
  line-height: 36px;
}
.ard-fans {
  width: 100%;
  margin-bottom: 20px;
}
.ard-fans-item {
  float: left;
  width: 65px;
  margin-right: 10px;
  margin-bottom: 15px;
}
.ard-fans-pic {
  display: block;
  width: 65px;
  height: 65px;
}
.ard-fans-item img {
  width: 65px;
  height: 65px;
}
.ard-fans-name {
  font-weight: 400;
  font-size: 13px;
  width: 100%;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ard-fans-txt {
  color: #999;
}
.ard-fans-txt:hover {
  color: var(--color-theme);
}
</style>